package com.brkj.test;

import android.content.Context;
import android.view.View;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.test.model.DS_Exam;
import com.brkj.util.ConstAnts;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseCoreActivity;
import com.brkj.util.view.BaseListFragment;
import com.brkj.util.view.BaseListView;
import com.dgl.sdk.util.DBStore;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalHttp;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by colin.ding on 2014/5/7.
 */
public class RealExamView extends BaseListView {

    private List<DS_Exam> examList =  new ArrayList<DS_Exam>();
    private List<DS_Exam> offlineExamList = new ArrayList<DS_Exam>();
    protected RealExamAdapter adapter;

    public RealExamView(Context context) {
        super(context);
    }

    @Override
    protected void fillView() {
        offlineExamList = DBStore.readAll(DS_Exam.class, ConstAnts.DB_TABLE_REALL_EXAM_OFFLINE_QUES_LIST);

        getExam();
    }

    public void show(){
        if(hasShown){
            return;
        }else{
            fillView();
            hasShown = true;
        }

    }


    private void getExam() {
        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetRealExam.params.UserID, MyApplication.myUserID);

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetRealExam.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)mContext) {


            @Override
            public void onSuccess(Object t) {
                // TODO Auto-generated method stub
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_Exam>>(){}.getType();
                examList = gson.fromJson((String)t, listType);

                fillContentView(examList, false);

            }

        });

    }



    private void fillContentView(List<DS_Exam> list, boolean isOffLineList){
        if((list != null) &&(list.size()>0)){

            mergeData(list);

            adapter = new RealExamAdapter(mContext, list, isOffLineList);
            listView.setAdapter(adapter);

            listView.setVisibility(View.VISIBLE);
            noContent.setVisibility(View.GONE);
        }else{
            listView.setVisibility(View.GONE);
            noContent.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 合并一些本地的数据
     * @param list
     */
    private void mergeData(List<DS_Exam> list) {

        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(DS_Exam offlineExam:offlineExamList){
            map.put(offlineExam.getExamPaperID(), offlineExam.getExamPaperID());
        }

        for(DS_Exam exam:list){
            if(map.containsKey(exam.getExamPaperID())){
                exam.setDownload(true);
            }
        }
    }


}